package com.zzyl.controller;

import com.zzyl.base.ResponseResult;
import com.zzyl.dto.BedDto;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bed")
@Api(tags = {"床位管理相关接口"})
public class BedController extends BaseController {

    @Autowired
    private BedService bedService;

    @GetMapping("/read/room/{roomId}")
    @ApiOperation(value = "根据房间ID查询床位", notes = "传入房间ID以获取该房间内的所有床位信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回床位列表"),
            @ApiResponse(code = 404, message = "未找到对应的床位信息")
    })
    public ResponseResult<List<BedVo>> readBedByRoomId(
            @ApiParam(value = "房间ID", required = true, example = "1")
            @PathVariable("roomId") Long roomId) {
        List<BedVo> beds = bedService.getBedsByRoomId(roomId);
        return success(beds);
    }

    /**
     * 创建床位
     */
    @ApiOperation(value = "创建床位", notes = "根据传入的床位信息创建新的床位")
    @PostMapping("/create")
    @ApiResponses({
            @ApiResponse(code = 200, message = "创建成功"),
            @ApiResponse(code = 500, message = "服务器错误")
    })
    public ResponseResult addBed(@RequestBody BedDto bedDto) {
        logger.info("创建床位: {}", bedDto);
        bedService.addBed(bedDto);
        return success();
    }

    /**
     * 根据ID查询床位
     */
    @ApiOperation(value = "根据ID查询床位", notes = "传入床位ID以获取床位详情")
    @GetMapping("/read/{id}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回床位详情"),
            @ApiResponse(code = 404, message = "未找到对应的床位")
    })
    public ResponseResult<BedVo> getBedById(
            @ApiParam(value = "床位ID", required = true, example = "1")
            @PathVariable Long id) {
        logger.info("根据ID查询床位: {}", id);
        BedVo bedVo = bedService.getBedById(id);
        return success(bedVo);
    }

    /**
     * 更新床位
     */
    @ApiOperation(value = "更新床位", notes = "根据传入的床位信息更新指定ID的床位")
    @PutMapping("/update")
    @ApiResponses({
            @ApiResponse(code = 200, message = "更新成功"),
            @ApiResponse(code = 404, message = "未找到对应的床位"),
            @ApiResponse(code = 500, message = "服务器错误")
    })
    public ResponseResult update(@RequestBody BedDto bedDto) {
        logger.info("更新床位: {}", bedDto);
        bedService.update(bedDto);
        return success();
    }

    /**
     * 删除床位
     */
    @ApiOperation(value = "删除床位", notes = "根据床位ID删除指定床位")
    @DeleteMapping("/delete/{id}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 404, message = "未找到对应的床位"),
            @ApiResponse(code = 500, message = "服务器错误")
    })
    public ResponseResult delete(
            @ApiParam(value = "床位ID", required = true, example = "1")
            @PathVariable Long id) {
        logger.info("删除床位: {}", id);
        bedService.delete(id);
        return success();
    }
}